/**
 * @Desc axios配置实例
 */
import axios from "axios";
import Message, { Notification } from "element-ui";

// 创建axios请求实例
const httpInstance = axios.create({
  timeout: 60000,
  headers: {
    "X-Requested-With": "XMLHttpRequest",
    "Content-Type": "application/json",
  },
});

enum Status {
  SUCCESS,

  // 1xxx 通用错误
  "系统错误" = 1000,
  "未登录",
  "用户名或密码错误",
  "无权限访问",
  "参数错误",
  无该记录,
  重定向认证中心,
  单点登录认证失败,
  错误access_token,
  缺少access_token,

  // 2xxx 实际模块错误
  ClientId已存在 = 2000,
  该用户名已注册,
}

httpInstance.interceptors.request.use(
  (config: any) => {
    return config;
  },
  (err: any) => {
    Promise.reject(err);
  }
);

httpInstance.interceptors.response.use(
  res => {
    return res.data;
  },
  (err: httpResponse) => {
    const { msg } = err.data;
    Message.error(msg);
    return Promise.reject(msg);
  }
);

export default class AJAX {
  /**
   * @Desc 处理response
   */

  static handleResponseData(res: any, resolve: Function) {
    let returnCode = res.resultCode ? res.resultCode : "1";
    switch (returnCode) {
      case "0":
        resolve(res.resultData);
        break;
      default:
        Notification({
          type: "error",
          title: "温馨提示",
          message: res.msg ? res.msg : "系统错误或者访问后台失败",
        });
        break;
    }
  }

  static Get(url: string, params = {}) {
    return new Promise((resolve, reject) => {
      httpInstance
        .get(url, { params })
        .then((res: any) => {
          // resolve(res);
          this.handleResponseData(res, resolve);
        })
        .catch((err: any) => {
          reject(err);
        });
    });
  }

  static Post(url: string, params = {}) {
    return new Promise((resolve, reject) => {
      httpInstance
        .post(url, JSON.stringify(params))
        .then((res: any) => {
          // resolve(res);
          this.handleResponseData(res, resolve);
        })
        .catch((err: any) => {
          reject(err);
        });
    });
  }

  static Put(url: string, params = {}) {
    return new Promise((resolve, reject) => {
      httpInstance
        .put(url, JSON.stringify(params))
        .then((res: any) => {
          // resolve(res);
          this.handleResponseData(res, resolve);
        })
        .catch((err: any) => {
          reject(err);
        });
    });
  }

  static Patch(url: string, params = {}) {
    return new Promise((resolve, reject) => {
      httpInstance
        .patch(url, JSON.stringify(params))
        .then((res: any) => {
          // resolve(res);
          this.handleResponseData(res, resolve);
        })
        .catch((err: any) => {
          reject(err);
        });
    });
  }

  static Delete(url: string, params = {}) {
    return new Promise((resolve, reject) => {
      httpInstance
        .delete(url, { data: JSON.stringify(params) })
        .then((res: any) => {
          // resolve(res);
          this.handleResponseData(res, resolve);
        })
        .catch((err: any) => {
          reject(err);
        });
    });
  }

  /**
   * @Desc Post接口上传
   */
  static Upload(url: string, params = {}) {
    return new Promise((resolve, reject) => {
      httpInstance
        .post(url, params, {
          headers: {
            "Content-Type": "multipart/form-data",
          },
        })
        .then((res: any) => {
          this.handleResponseData(res, resolve);
        })
        .catch((err: any) => {
          reject(err);
        });
    });
  }
}
